from fastapi import APIRouter, Request
from app.parameter import CreateGroupParam,DeleteGroupParam, QueryGroupParam, GroupInvataionParam, CheckInvatationParam
from app.service import GroupService
from app.utils import getUserInfo

router = APIRouter(tags=["群组路由"])
from utils import DEBUG

# 创建群组
@router.post("/group/create")
async def create(createGroupParam: CreateGroupParam, request: Request):
    DEBUG(createGroupParam)
    createGroupParam.userId = getUserInfo(request)
    return GroupService.createGroup(createGroupParam)

# 解散群组
@router.post("/group/delete")
async def delete(deleteGroupParam: DeleteGroupParam, request: Request):
    DEBUG(deleteGroupParam)
    deleteGroupParam.userId = getUserInfo(request)
    return GroupService.deleteGroup(deleteGroupParam)

@router.post("/group/querys")
async def querys(request: Request):
    userId = getUserInfo(request)
    return GroupService.getGroups(userId)

@router.post("/group/query")
async def query(queryGroupParam: QueryGroupParam, request: Request):
    DEBUG(queryGroupParam)
    queryGroupParam.userId = getUserInfo(request)
    return GroupService.getGroup(queryGroupParam)

@router.post("/group/exit")
async def exit(queryGroupParam: QueryGroupParam, request: Request):
    DEBUG(queryGroupParam)
    queryGroupParam.userId = getUserInfo(request)
    return GroupService.exitGroup(queryGroupParam)

@router.post("/group/getInvationCode")
async def getInvation(groupInvationParam: GroupInvataionParam, request: Request):
    DEBUG(groupInvationParam)
    groupInvationParam.userId = getUserInfo(request)
    return GroupService.getInvationCode(groupInvationParam.groupId, \
        groupInvationParam.userId, groupInvationParam.permission, None)

@router.post("/group/join")
async def index(checkInvatationParam: CheckInvatationParam, request: Request):
    DEBUG(checkInvatationParam)
    checkInvatationParam.userId = getUserInfo(request)
    return GroupService.checkInvationCode(checkInvatationParam.signature, checkInvatationParam.userId)